package com.zhulong.saas.cloud.gateway.controller;

import com.zhulong.common.basic.dto.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.time.Duration;

@RestController
@RequestMapping("/cache")
public class CacheManagerController {

    @Autowired
    private RedisCacheManager cacheManager;

    @PostMapping("/evitCache")
    public Mono<Response<Void>> evict(String cacheName, String cacheKey) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            cache.evict(Long.valueOf(cacheKey));
        }
        return Mono.just(Response.success(null));
    }

    public static void main(String[] args) {
        Duration duration = Duration.ofMillis(1);
        System.out.println(duration);
    }

}
